import jwt
from jwt import ExpiredSignatureError
from catering import settings
from utils.RedisTools import RedisTool


class JwtTool:
    def create_jwt(self,user):
        payload={
            'id':user.id
        }
        key = settings.SECRET_KEY
        token=jwt.encode(payload,key,algorithm='HS256')
        return token

    def valid_jwt(self,token):
        try:
            r=RedisTool().rds
            if r.exists(f'token_{token}'):
                return {}

            key = settings.SECRET_KEY
            payload=jwt.decode(token,key,algorithms='HS256')
            # print(1234)
            return payload
        except ExpiredSignatureError as e:
            print(e)
            return None
        except Exception as e:
            print(e)
            return {}


